import { apiConstants,phoneReg,loadModalInfo } from '@/js/utils'
new Vue({
  el: '#app',
  data: {
    code:'',
    phone:'',
    remainTime: 0,
  },
  created(){

  },
  computed: {
    isDisableLogin() {
      if(!this.phone || !this.code){
        return true
      }else{
        return false
      }
    },
    isCanGetCode(){
      if (phoneReg.test(this.phone)){
        return true
      }else{
        return false
      }
    }
  },
  methods:{
    getCheckCode(){
      if (!phoneReg.test(this.phone)){
        loadModalInfo("请输入正确手机号","/images/login/icon-tixing.png");
        return;
      }
      this.$ajax(apiConstants.login_getVerificationCode, {
        data: { phone: this.phone },
        success: data => {
          if(data){
            loadModalInfo(data.message);
            return;
          }
          //短信发送成功才走倒计时
          this.codeTime();
        }
      })
    },
    codeTime(){
      if (this.remainTime > 0) return
      this.remainTime = 60
      clearInterval(this.inv)
      this.inv = setInterval(() => {
        if (this.remainTime <= 0) {
          clearInterval(this.inv)
          return
        }
        this.remainTime--
      }, 1000)
    },
    doLogin(){
      if (!this.validate()) return
      this.$Toast.start();
      axios.post('/doLogin', {
        verificationCode:this.code,
        phone:this.phone,
      }).then(({ data }) => {
        this.$Toast.finish();
        if (data.code == 200) {
          location.href = !data.enterUrl?"/index":data.enterUrl;
        }else if(data.code == 300){
          loadModalInfo("该账号已登录，请退出后重新登录！");
        }else if(data.code == 201){
          this.$Message({
            content: `手机号或验证码有误`,
            time: 1500,
            type: 'default',
            hasClose: true
          });
        }else{
          loadModalInfo(data.message)
        }
      }).catch(e => {
        this.$Toast.finish()
        loadModalInfo({ content: e.message })
      })
    },
    phoneClick(){
      $("#phoneInput").focus();
    },
    codeClick(){
      $("#codeInput").focus();
    },
    validate() {
      let content = null
      if (!this.phone) {
        content = '请输入手机号';
      }else if (!this.code) {
        content = '请输入验证码';
      }

      if (!content) {
        return true
      }else{
        return false
      }
    },
  }
});
